/**
 * Project Name:template-web
 * File Name   :AESSecurityUtil.java
 * Package Name:org.cn.template.component.utils
 * Date:2015年9月27日下午4:18:18
 * Copyright (c) 2015, http://my.oschina.net/httpssl All Rights Reserved.
 *
*/

package org.cn.template.component.utils;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/**
 * ClassName:AESSecurityUtil <br/>
 * Function :TODO ADD FUNCTION. <br/>
 * Reason :TODO ADD REASON. <br/>
 * Date :2015年9月27日 下午4:18:18 <br/>
 * 
 * @author :http://my.oschina.net/httpssl
 * @email :491835898@QQ.COM
 * @since :JDK 1.7
 * @see
 */
public class AESSecurityUtil
{
	/** 密钥算法 */
	private static final String	KEY_ALGORITHM	= "AES";
	private static final int	KEY_SIZE		= 128;

	/** 加密/解密算法/工作模式/填充方法 */
	// public static final String CIPHER_ALGORITHM = "AES/ECB/NoPadding";
	/**
	 * 获取密钥
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Key getKey() throws Exception {
		// 实例化
		KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
		// AES 要求密钥长度为128位、192位或256位
		kg.init(KEY_SIZE);
		// 生成密钥
		SecretKey secretKey = kg.generateKey();
		return secretKey;
	}

	/**
	 * 转化密钥
	 * 
	 * @param key
	 *            密钥
	 * @return Key 密钥
	 * @throws Exception
	 */
	public static Key codeToKey(String key) throws Exception {
		byte[] keyBytes = Base64.decodeBase64(key);
		SecretKey secretKey = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
		return secretKey;
	}

	/**
	 * 解密
	 * 
	 * @param data
	 *            待解密数据
	 * @param key
	 *            密钥
	 * @return byte[] 解密数据
	 * @throws Exception
	 */
	private static String decrypt(byte[] data, byte[] key) throws Exception {
		// 还原密钥
		Key k = new SecretKeySpec(key, KEY_ALGORITHM);
		/**
		 * 实例化 使用PKCS7Padding填充方式，按如下方式实现
		 * Cipher.getInstance(CIPHER_ALGORITHM,"BC");
		 */
		Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
		// 初始化，设置解密模式
		cipher.init(Cipher.DECRYPT_MODE, k);
		// 执行操作
		return new String(cipher.doFinal(data), "UTF-8");
	}

	/**
	 * 解密
	 * 
	 * @param data
	 *            待解密数据
	 * @param key
	 *            密钥
	 * @return byte[] 解密数据
	 * @throws Exception
	 */
	public static String decrypt(String data, String key) throws Exception {
		return decrypt(Base64.decodeBase64(data), Base64.decodeBase64(key));
	}

	/**
	 * 加密
	 * 
	 * @param data
	 *            待加密数据
	 * @param key
	 *            密钥
	 * @return bytes[] 加密数据
	 * @throws Exception
	 */
	public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
		// 还原密钥
		Key k = new SecretKeySpec(key, KEY_ALGORITHM);
		/**
		 * 实例化 使用PKCS7Padding填充方式，按如下方式实现
		 * Cipher.getInstance(CIPHER_ALGORITHM,"BC");
		 */
		Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
		// 初始化，设置为加密模式
		cipher.init(Cipher.ENCRYPT_MODE, k);
		// 执行操作
		return cipher.doFinal(data);
	}

	public static String encrypt(String data, String key) throws Exception {
		byte[] dataBytes = data.getBytes("UTF-8");
		byte[] keyBytes = Base64.decodeBase64(key);
		return Base64.encodeBase64String(encrypt(dataBytes, keyBytes));
	}

	/**
	 * 初始化密钥
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String getKeyStr() throws Exception {
		return Base64.encodeBase64String(getKey().getEncoded());
	}

	/**
	 *
	 * 创建日期2011-4-25上午10:12:38 修改日期 作者：dh *TODO 使用Base64加密算法加密字符串 return
	 */
	public static String encodeStr(String plainText) {
		byte[] b = plainText.getBytes();
		Base64 base64 = new Base64();
		b = base64.encode(b);
		String s = new String(b);
		return s;
	}

	/**
	 *
	 * 创建日期2011-4-25上午10:15:11 修改日期 作者：dh *TODO 使用Base64加密 return
	 */
	public static String decodeStr(String encodeStr) {
		byte[] b = encodeStr.getBytes();
		Base64 base64 = new Base64();
		b = base64.decode(b);
		String s = new String(b);
		return s;
	}

	public static void main(String[] args) throws Exception {
		String key = "VxDksHQiTvQt9MMPtMVXdA==";
		StringBuffer buffer = new StringBuffer();
		buffer.append("user_id=1,time=20150516,state=2,user_id=1,time=阿萨德撒啊三大啊三大阿斯");
		String jimm = buffer.toString();
		String mw = AESSecurityUtil.encrypt(jimm, key);
		System.out.println("密文:" + mw);
		String jm = AESSecurityUtil.decrypt(mw, key);
		System.out.println("明文:" + jm);
	}
}
